clear
%% PROBLEM
d = 2;
x_bound = repmat([-6,6],[d,1]);
OptSet.sol = [3,2;
    -2.805118,3.131312;
    -3.779310,-3.283186;
    3.584428,-1.848126];
OptSet.fval = 0;
OptSet.radius = 0.1;
precision_init = 0.4; % 3.848
MaximalBudget = 50000;
precision = [0.074, 0.024, 0.0074, 0.0024];

rep = 1;

Problem.Dimension = d; % the dimension of the decision variable
Problem.LowerBound = x_bound(:,1)'; % the lower bound of the decision variable
Problem.UpperBound = x_bound(:,2)'; % the upper bound of the decision variable
Problem.ObjFunc = @ObjValue;  % the objective function
%% ALGORITHM
Algorithm.PopulationSize = 100;  % the population size of the ga
% the followings are not necessary
Algorithm.F = 0.5;  % the mutation factor of the differential evolution
Algorithm.CR = 0.9;  % crossover index of the differential evolution
Algorithm.T = 40; % 80 for high dimension  % the threshold of the number of generations that solutions are not improved
Algorithm.MaxFEs = 5000;%MaximalBudget; % the maximal budget

tic
[population, EvaluationNum, GenerationNum] = dsde(Problem, Algorithm);
toc
GenerationNum
EvaluationNum

%% FIGURES
if rep == 1
    if d==1
        x=x_bound(1,1):(x_bound(1,2)-x_bound(1,1))/1000:x_bound(1,2);
        z=ObjValue(x');
        z_max=max(z);
        z_min=min(z);
        figure()
        hold on
        plot(x,z)
        plot(population(:,2),population(:,1),'.')
        hold off
    end
    if d == 2
        figure()
        hold on
        scatter(population(:,2),population(:,3),2,population(:,1),'filled');
        plot(OptSet.sol(:,1),OptSet.sol(:,2),'o')
        hold off
        xlabel('','Interpreter','latex','String','$x_1$')
        ylabel('','Interpreter','latex','String','$x_2$')
        set(gca,'Fontname','Times New Roman','FontSize',16);
    end
end
